import { Mutation , Action } from 'vuex';
import { StoreModuleType } from '../store';
import localforage from  'localforage'
//localforage.setDriver(localforage.LOCALSTORAGE)
const sidebarShowTypeKey = "sidebarShowType"

let getLocalStorageSidebarShowType = function(){
  const local_SidebarShowType = localStorage.getItem(sidebarShowTypeKey)
  let init_SidebarShowType = SidebarShowType.Normal
  if(local_SidebarShowType == '2'){
    init_SidebarShowType = SidebarShowType.Mini
  }else if(local_SidebarShowType == '3'){
    init_SidebarShowType = SidebarShowType.Hide
  }
  return init_SidebarShowType
}

export interface StateType {
  // 左侧展开收起
  sidebarShowType: SidebarShowType;
}

export enum SidebarShowType {
  Normal = 1,
  Mini = 2, // 收缩
  Hide = 3, // 隐藏
}


export interface ModuleType extends StoreModuleType<StateType> {
  state: StateType;
  mutations: {
    setSidebarShowType: Mutation<StateType>;
  };
  actions: {
    setSidebarShowType: Action<StateType, StateType>;
  };
}


const initState: StateType = {
  sidebarShowType:  getLocalStorageSidebarShowType(), //SidebarShowType.Normal
};

const StoreModel: ModuleType = {
  namespaced: true,
  name: 'global',
  state: {
    ...initState
  },
  mutations: {
    setSidebarShowType(state, sidebarShowType:SidebarShowType) {
      // store.commit('mutations方法名',值)
      state.sidebarShowType = sidebarShowType;
      localStorage.setItem(sidebarShowTypeKey, state.sidebarShowType.toString())
    }
  },
  actions: {
    async setSidebarShowType({ commit }, payload: SidebarShowType) {
      //State.dispatch('global/setSidebarShowType', SidebarShowType.Mini);
      commit('setSidebarShowType', payload);
    },
  }
}

export default StoreModel;
